/*
 * transitionsSelectionWindow.java
 *
 * Created on 7 maj 2007, 22:40
 */

package edu.uj.petri.gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import edu.uj.petri.model.Transition;
import java.util.ResourceBundle;

/**
 *
 * @author  MonikaG
 */
public class TransitionsSelectionWindow extends javax.swing.JDialog {
    
    private static final long serialVersionUID = 3456546542688539999L;
    private DefaultListModel allTransitionsListModel;
    private DefaultListModel selectedTransitionsListModel;
    
    /** Creates new form transitionsSelectionWindow */
    public TransitionsSelectionWindow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.delaySpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 60, 1));
        
        // set window size
        setSize(new java.awt.Dimension(480, 400));
        
        this.allTransitionsListModel = new DefaultListModel();
        this.allTransitionsList.setModel(this.allTransitionsListModel);
        this.selectedTransitionsListModel = new DefaultListModel();
        this.selectedTransitionsList.setModel(this.selectedTransitionsListModel);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        allTransitionsList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        selectedTransitionsList = new javax.swing.JList();
        addSelectedTransitions = new javax.swing.JButton();
        addAllTransitions = new javax.swing.JButton();
        finishButton = new javax.swing.JToggleButton();
        delSelectedTransition = new javax.swing.JButton();
        upSeletectedTransition = new javax.swing.JButton();
        downSeletectedTransition = new javax.swing.JButton();
        delayLabel = new javax.swing.JLabel();
        delaySpinner = new javax.swing.JSpinner();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Select_transitions")); // NOI18N
        allTransitionsList.setToolTipText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("list_of_all_transition_in_petri_net")); // NOI18N
        jScrollPane1.setViewportView(allTransitionsList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.25;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 9);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        selectedTransitionsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        selectedTransitionsList.setToolTipText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("list_of_transitions_to_fire")); // NOI18N
        jScrollPane2.setViewportView(selectedTransitionsList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 9, 3, 9);
        getContentPane().add(jScrollPane2, gridBagConstraints);

        addSelectedTransitions.setText(">");
        addSelectedTransitions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSelectedTransitionsActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(9, 0, 3, 0);
        getContentPane().add(addSelectedTransitions, gridBagConstraints);

        addAllTransitions.setText(">>");
        addAllTransitions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAllTransitionsActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
        getContentPane().add(addAllTransitions, gridBagConstraints);

        finishButton.setText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Finish")); // NOI18N
        finishButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 9);
        getContentPane().add(finishButton, gridBagConstraints);

        delSelectedTransition.setText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Del")); // NOI18N
        delSelectedTransition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delSelectedTransitionActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(9, 0, 3, 0);
        getContentPane().add(delSelectedTransition, gridBagConstraints);

        upSeletectedTransition.setText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Up")); // NOI18N
        upSeletectedTransition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upSeletectedTransitionActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        getContentPane().add(upSeletectedTransition, gridBagConstraints);

        downSeletectedTransition.setText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Dn")); // NOI18N
        downSeletectedTransition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downSeletectedTransitionActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
        getContentPane().add(downSeletectedTransition, gridBagConstraints);

        delayLabel.setText(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Delay_[s]:")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 12, 0);
        getContentPane().add(delayLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0);
        getContentPane().add(delaySpinner, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void finishButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishButtonActionPerformed
        dispose();
    }//GEN-LAST:event_finishButtonActionPerformed

    private void downSeletectedTransitionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downSeletectedTransitionActionPerformed
        Object tmp   = null;
        int    index = this.selectedTransitionsList.getSelectedIndex();
        if (index >= this.selectedTransitionsListModel.size()-1)
            return;
        tmp = this.selectedTransitionsListModel.remove(index);
        this.selectedTransitionsListModel.add(index+1, tmp);
        this.selectedTransitionsList.setSelectedIndex(index+1);
    }//GEN-LAST:event_downSeletectedTransitionActionPerformed

    private void upSeletectedTransitionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upSeletectedTransitionActionPerformed
        Object tmp   = null;
        int    index = this.selectedTransitionsList.getSelectedIndex();
        if (index < 1)
            return;
        tmp = this.selectedTransitionsListModel.remove(index);
        this.selectedTransitionsListModel.add(index-1, tmp);
        this.selectedTransitionsList.setSelectedIndex(index-1);
    }//GEN-LAST:event_upSeletectedTransitionActionPerformed

    private void delSelectedTransitionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delSelectedTransitionActionPerformed
        int index = this.selectedTransitionsList.getSelectedIndex();
        
        if (index < 0)
            return;
        
        this.selectedTransitionsListModel.remove(index);
        
        if (index < this.selectedTransitionsListModel.size())
            this.selectedTransitionsList.setSelectedIndex(index);
        else if (this.selectedTransitionsListModel.size() != 0)
            this.selectedTransitionsList.setSelectedIndex(index-1);
    }//GEN-LAST:event_delSelectedTransitionActionPerformed

    private void addAllTransitionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAllTransitionsActionPerformed
        for (int i = 0; i < this.allTransitionsListModel.size() ; i++) {
            this.selectedTransitionsListModel.addElement(this.allTransitionsListModel.getElementAt(i));
        }
    }//GEN-LAST:event_addAllTransitionsActionPerformed

    private void addSelectedTransitionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSelectedTransitionsActionPerformed
        for (int i = 0; i < this.allTransitionsList.getSelectedValues().length; i++) {
            this.selectedTransitionsListModel.addElement(this.allTransitionsList.getSelectedValues()[i]);
        }
    }//GEN-LAST:event_addSelectedTransitionsActionPerformed
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAllTransitions;
    private javax.swing.JButton addSelectedTransitions;
    private javax.swing.JList allTransitionsList;
    private javax.swing.JButton delSelectedTransition;
    private javax.swing.JLabel delayLabel;
    private javax.swing.JSpinner delaySpinner;
    private javax.swing.JButton downSeletectedTransition;
    private javax.swing.JToggleButton finishButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList selectedTransitionsList;
    private javax.swing.JButton upSeletectedTransition;
    // End of variables declaration//GEN-END:variables
 
    /**
     * Returns selected transitions list.
     *
     * @return selected transitions list
     */
    public List<Transition> getSelectedTransitions() {
        ArrayList<Transition> result = new ArrayList<Transition>();
        Object[] tmp = this.selectedTransitionsListModel.toArray();
        
        for (int i = 0; i < tmp.length; i++)
            result.add((Transition)tmp[i]);
        
        return result;
    }
    
    public int getDelay() {
        return ((Integer)this.delaySpinner.getValue()).intValue();
    }
    
    /** 
     * Add transition to the available transitions list.
     *
     * @param transition transition to add
     */
    public void add(Transition transition) {
        this.allTransitionsListModel.addElement(transition);
    }
    
    /** 
     * Remove transition from the available transitions list.
     *
     * @param transition transition to remove
     */
    public void remove(Object transition) {
        this.allTransitionsListModel.removeElement(transition);
    }
}
